<?php 
/* ================================================================================
 * LivingWeb	 
 *
 * page_open.php: Allows the logged on user to select a page to open.
 *
 * Author: Eric Hendrickson (enhendrickson@liberty.edu)
 * Date: 3/1/05
   ================================================================================ */
		
	//Check to see if there is a valid session 
	$SessionInfo = $SSO->isValidSession("index.php?action=login", $SSO->getSessionInformation());	
	
	//Check to see if the user has rights to be on LivingWeb
	$PageRights = array('ADMIN', 'USER');
	$UserRoles = $Security->ValidateUser($SessionInfo['username'], $AppID, $PageRights, 1);
	
	//Grab the user info
	$UserInfo = $User->GetUserInfo($SessionInfo['username']);	
	
	//if this is not a repost then the $_POST['selMinistry'] did not get created or set
	if(!isset($_POST['selMinistry']) || $_POST['selMinistry'] =="")
	{
		//get the ministries that the user is a member of
		$Ministries = $PageServer->GetMinistryByUser($SessionInfo['username']);
		//get the keys (the MinistryIDs) from the Ministries array
		$FirstMinistry = array_keys($Ministries);
		//store the $_POST var which did not exist until now
		$_POST['selMinistry'] = $FirstMinistry[0];	
	}
	
	//functions which pertain to this file only
	function GenerateMinistryOptions($selected)
	{
		global $SessionInfo, $PageServer;
		$OptionsList = "";
		foreach($PageServer->GetMinistryByUser($SessionInfo['username']) as $MinistryID => $MinistryName)
		{
			$OptionsList = $OptionsList . "<option value='" . $MinistryID . "'";
			if($selected==$MinistryID)
			{
				$OptionsList = $OptionsList . "selected>" . $MinistryName . "</option>";
			}
			else
			{
				$OptionsList = $OptionsList . ">" . $MinistryName . "</option>";
			}
		}
		print $OptionsList;
		return;
	}

	function GenerateMinistryPages($ministry_id)
	{
		global $SessionInfo, $PageServer;
		$PageGroup = $PageServer->GetPagesByMinistryID($ministry_id);
		$OptionsList = "";
		foreach($PageGroup['pages'] as $PageID => $PageTitle)
		{
			$OptionsList = $OptionsList . "<option value='" . $PageID . "'>" . $PageTitle . "</option>";
		}
		print $OptionsList;
		return;
	}
?>

	<form action="index.php?action=edit" method="post" name="frmOpenPage">	
	<?php 
		$OpenWindow = new window;
		$OpenWindow->title = "Open Page";
		$OpenWindow->width = "500";
		$OpenWindow->start_window(); 
	?>
	<table cellpadding="0" cellspacing="0" width="500" align="center" border="0">
		<tr>
			<td>
				<img src="images/1pixel.gif" height="15" alt="" border="0">
			</td>
		</tr>
		<tr>
			<td colspan="3">
				<table cellpadding="0" cellspacing="0" width="90%" align="center" border="0">
					<tr>
						<td>
							<span class="LivingWebTextBold">Select Ministry</span>&nbsp;<select name="selMinistry" class="LivingWebText" onChange="frmOpenPage.action = 'index.php?action=open'; frmOpenPage.submit();"><?php GenerateMinistryOptions($_POST['selMinistry']); ?></select>
						</td>
					</tr>
					<tr>
						<td>
							<img src="images/1pixel.gif" height="15" alt="" border="0">
						</td>
					</tr>
					<tr>
						<td>
							<table width="100%" cellpadding="0" cellspacing="0" border="0">
								<tr>
									<td>
										<select name="selPages"  class="LivingWebText"  multiple size="20" style="width:100%;"><?php GenerateMinistryPages($_POST['selMinistry']); ?></select>
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>						
			</td>
		</tr>
		<tr>
			<td align="right" width="400"><input type="submit" value="Open Page" name="btnOpen" class="LivingWebButton"></td>
			<td><img src="images/1pixel.gif"  height="40" width="20" border="0" alt=""></td>
			<td width="400" valign="middle"><input type="button" value="Cancel" name="btnCancel" class="LivingWebButton" onClick="location.href='index.php?action=main';"></td>
		</tr>
	</table>
	<?php	$OpenWindow->end_window(); ?>	
	</form>
